package org.zs.asp04;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ApsLog {
    /**
     * 切点表达式的提取和重用
     * 1 定义一个方法 @Pointcut("execution(* org..aop04..Impl.*.*(..))")
     *2 通知的位置直接引用方法即可
    * */
//    @Pointcut("execution(* org..aop04..Impl.*.*(..))")
//    public void pc(){}

    @Before(value = "org.zs.asp04.CustomPoint.pc1()")
    public void begin(JoinPoint joinPoint)
    {
        // 获取目标对象（当前方法所属的对象）       
        Object target = joinPoint.getTarget();
        // 目标类的名称
        String name = target.getClass().getName();
        // 获取目标对象的名称
        String methodName = joinPoint.getSignature().getName();
        // 获取目标对象的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("类：" + name + "方法：" + methodName + "传入参数：" + args.toString() + "开始执行......");
        System.out.println("ApsLog方法执行之前");
    }
    // 结束通知 @AfterReturning
    @AfterReturning(value = "org.zs.asp04.CustomPoint.pc1()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result)
    {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("类：" + className + "方法：" + methodName + "正常执行完毕了.......返回值：" + result);
    }

    // 报错通知 @AfterThrowing
    @AfterThrowing(value = "org.zs.asp04.CustomPoint.pc1()", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Throwable e)
    {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("类：" + className + "方法：" + methodName + "异常了.......异常信息：" + e.getMessage());
    }

}
